<template>
  <nav class="app-topnav">
    <div class="container">
      <ul>
        <template v-if="profile.token">
          <li><a href="javascript:;"><i class="iconfont icon-user"></i>{{profile.account}}</a></li>
          <li><a href="javascript:;" @click="loginOut">退出登录</a></li>
        </template>
        <template v-else>
          <li><RouterLink to="/login">请先登录</RouterLink></li>
          <li><a href="javascript:;">免费注册</a></li>
        </template>
        <li><a href="javascript:;">我的订单</a></li>
        <li><a href="javascript:;">会员中心</a></li>
        <li><a href="javascript:;">帮助中心</a></li>
        <li><a href="javascript:;">关于我们</a></li>
        <li><a href="javascript:;"><i class="iconfont icon-phone"></i>手机版</a></li>
      </ul>
    </div>
  </nav>
</template>

<script>
import { computed } from 'vue'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
import { ElMessageBox, ElMessage } from 'element-plus'
export default {
  name: 'navbarTop',
  setup () {
    // 获取用户登录信息
    const store = useStore()
    // const { profile } = store.state.user
    const profile = computed(() => {
      return store.state.user.profile
    })
    const router = useRouter()
    const loginOut = () => {
      ElMessageBox.confirm(
        '是否要退出登陆',
        {
          confirmButtonText: '确认',
          cancelButtonText: '取消',
          type: 'warning'
        }
      )
        .then(() => {
          // 把用户信息清空
          store.commit('user/setUser', {})
          // 把本地购物车清空
          store.commit('cart/setCart', [])
          router.push('/login')
          ElMessage({
            type: 'success',
            message: '退出成功'
          })
        })
        .catch(() => {
          ElMessage({
            type: 'info',
            message: '取消退出'
          })
        })
    }
    return { profile, loginOut }
  }
}
</script>

<style lang="less" scoped>
@import '../assets/styles/variable.less';
.app-topnav {
  background: #333;
  ul {
    display: flex;
    height: 53px;
    justify-content: flex-end;
    align-items: center;
    li {
      a {
        padding: 0 15px;
        color: #cdcdcd;
        line-height: 1;
        display: inline-block;
        i {
          font-size: 14px;
          margin-right: 2px;
        }
        &:hover {
          color: @c;
        }
      }
      ~ li {
        a {
          border-left: 2px solid #666;
        }
      }
    }
  }
}
</style>
